<template>
	<view class="imgCont" style="border: none;">
		<view class="main">
			<!-- 一级分类 -->
			<view class="active_tit paddingNone">
				<view class="imgTit">
					<slot></slot> 
				</view>
				<view style="display: flex;">
					<view class="imgBtn" @click="upTree">
						更新押品信息影像目录
					</view>
					<!-- <view class="imgBtn" @click="isShow">
						{{showText}}
					</view> -->
					
				</view>
				
			</view>
			<!-- 二级分类 -->
			<block >
				<block v-for="item in treeData">
					<view class="active_tit paddingNone">
						<view class="imgTit2">
							{{item.label}}
						</view>
						<view class="imgBtn" @click="isShowItem(item)">
							{{item.showText}}
						</view>
					</view>
					<block v-if="item.show">
						<block v-for="eitem in item.children">
							<view class="active_tit paddingNone">
								<view class="imgTit3">
									{{eitem.label}}
								</view>
								<view class="imgBtn" @click="isShowItem(eitem)">
									{{eitem.showText}}
								</view>
							</view>

							<block v-if="eitem.show">
								<block v-for="zitem in eitem.children">
									<view class="active_tit paddingNone">
										<view class="imgTit3">
											{{zitem.label}}
										</view>
									</view>
									<block v-for="imgItem in imageData[zitem.code]">
										<view class="choose-img-box"
											v-if="imgItem.base64!=''&&imgItem.businessTag==zitem.name">
											<icon v-if="imgItem.base64!=''" class="icon" type="cancel" size="26"
												@click="remove(imageData[zitem.code],imgItem,zitem.code)" />
											<image class="img" :src="imgItem.img" @click="lookImg(imgItem.img)" />
										</view>

									</block>
									<view class="choose-img-box">
										<image class="img" src="/static/enterprise_card.png"
											@click="chooseImg(imageData[zitem.code],zitem.code,zitem.name)" />
									</view>
								</block>
							</block>
						</block>


					</block>


				</block>
			</block>

		</view>

	</view>

</template>

<script>
	import chooseImgToBase64 from "@/utils/chooseImgToBase64";
	export default {
		data() {
			return {
				showText: '展开',
				show: false,
				treeData: [{
						label: '①抵（质）押材料',
						showText: '展开',
						children: [

						]

					}

				],

				imageData: {
					SDMS_0501_29: [{
						img: '/static/enterprise_card.png',
						base64: ''
					}],
					SDMS_0501_31: [{
						img: '/static/enterprise_card.png',
						base64: ''
					}],
					SDMS_0502_32: [{
						img: '/static/enterprise_card.png',
						base64: ''
					}],
					SDMS_0502_32: [{
						img: '/static/enterprise_card.png',
						base64: ''
					}],
					SDMS_0501_06:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0501_07:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0502_33:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0502_34:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0502_35:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0502_36:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0502_37:[{img:'/static/enterprise_card.png',base64:''}],
				}
			}
		},
		props: {
			changeFlag: {
				type: Number
			},
		},
		watch: {
			changeFlag: {
				handler: function(res) {
					//console.warn('数据变化',res)
					this.imageData = getApp().globalData.imageData.dywData

				},
				deep: true
			},
			imageData: {
				handler: function(res) {
					//console.warn('数据变化',res)
					getApp().globalData.imageData.dywData = res
				},
				deep: true
			}
		},
		mounted() {
			this.fileQuery('SDMS_0501')
			this.fileQuery('SDMS_0502')
			setTimeout(()=>{
				this.upTree()
			},2000)
			



		},
		methods: {
			fileQuery(code) {

				let obj = {
					typeCode: code,
					businessId: getApp().globalData.stdOffsurBaiscInfo.businessId,
				}
				this.sendRequest({
					url: 'ext/findImageList', //'offsur/fileQuery',
					method: 'post',
					hideLoading: true,
					data: obj,
					complete: (res) => {
						let list = Object.keys(this.imageData)
						let imgData = []
						if (res.code == '200') {
							res.data.map((item, key) => {
								item.img = 'data:image/png;base64,' + item.fileBase64
								item.base64 = item.fileBase64
							})

							//console.log(res.data)

							for (let i = 0; i < res.data.length; i++) {
								for (let j = 0; j < list.length; j++) {
									if (res.data[i].typeCode == list[j]) {
										this.imageData[list[j]].push(res.data[i])
									}
								}
							}

						}
					}
				})
			},
			chooseImg(name, code, userName) {

				chooseImgToBase64(
					(base64, base64SourceData, file) => {
						//console.log(base64SourceData)
						let imgData = {
							img: base64,
							base64: base64SourceData
						}

						//this.idCardOcr("FRONT");
						this.fileUpload(code, file.name, base64SourceData, imgData, name, userName)

					}, 2048

				);
			},
			fileUpload(code, fileName, base64, item, name, userName) {
				let obj = {
					businessTag: userName,
					fileBase64: base64,
					businessId: getApp().globalData.stdOffsurBaiscInfo.businessId,
					typeCode: code,
					fileName: fileName,
					currentUserId: getApp().globalData.stdOffsurBaiscInfo.customerId
				}
				this.sendRequest({
					url: 'ext/upload',
					method: 'post',
					hideLoading: false,
					data: obj,
					complete: (res) => {

						if (res.code == '200') {
							item.fileId = res.data.fileId
							item.businessTag = userName

							name.push(item)
						} else {
							uni.showToast({
								icon: 'none',
								title: "图片上传失败，请重试",
								duration: 2000
							})
						}
					}
				})
			},
			remove(name, item) {
				let obj = {
					businessId:getApp().globalData.stdOffsurBaiscInfo.businessId,
					fileId: item.fileId,
					currentUserId: getApp().globalData.stdOffsurBaiscInfo.customerId
				}
				this.sendRequest({
					url: 'ext/deleteImage',
					method: 'post',
					hideLoading: true,
					data: obj,
					complete: (res) => {

						if (res.code == '200') {
							name.remove(item)
						}
					}
				})

			},
			isShow() {
				if (this.show) {
					this.show = false
					this.showText = '展开'
				} else {
					this.show = true
					this.showText = '收起'
				}
			},
			isShowItem(item) {
				if (item.show) {
					item.show = false
					item.showText = '展开'
				} else {
					item.show = true
					item.showText = '收起'
				}
			},
			subSet(arr1, arr2) {
				let newArr1 = []
				let newArr2 = []
				for(let i=0;i<arr1.length;i++){
					newArr1.push(arr1[i].mortgageSystemId)
				}
				for(let j=0;j<arr2.length;j++){
					newArr2.push(arr2[j].label)
				}
				
				let set1 = new Set(newArr1);
				let set2 = new Set(newArr2);
			
				let subset = [];
			
				for (let item of set2) {
					if (!set1.has(item)) {
						subset.push(item);
					}
				}
				let arrItem = []
				for(let m=0;m<subset.length;m++){
					for(let n=0;n<arr2.length;n++){
						if(subset[m] == arr2[n].label){
							arrItem.push(arr2[n])
						}
						
					}
				}
			
				return arrItem;
			},
			//通过抵押物数据,动态生成目录
			upTree() {
				////获取担保主体信息
				let arr = [
					...getApp().globalData.stdOffsurCollateral03,
					...getApp().globalData.stdOffsurCollateral04
				]
				for (let i = 0; i < arr.length; i++) {
					if (arr[i].mortgageSystemId == '') {
						uni.showToast({
							icon: 'none',
							title: '请先生成押品编号！',
							duration: 2500
						})
						return false
					}

					let item = {
						label: arr[i].mortgageSystemId,
						showText: '展开',
						children: [{
								name: arr[i].mortgageSystemId,
								label: '抵（质）押人身份证明',
								code: 'SDMS_0501_29'
							},
							{
								name: arr[i].mortgageSystemId,
								label: '征信授权书',
								code: 'SDMS_0501_06'
							},
							{
								name: arr[i].mortgageSystemId,
								label: '抵（质）押人征信报告',
								code: 'SDMS_0501_07'
							},
							{
								name: arr[i].mortgageSystemId,
								label: '抵质押人声明',
								code: 'SDMS_0501_31'
							},
							{
								name: arr[i].mortgageSystemId,
								label: '抵（质）押物所有权（含共有权）、使用权证或证明文件材料',
								code: 'SDMS_0502_32'
							},
							{
								name: arr[i].mortgageSystemId,
								label: '已办理抵（质）押登记的他项权证或有关证明文件',
								code: 'SDMS_0502_33'
							},
							{
								name: arr[i].mortgageSystemId,
								label: '权利质押中的各种有价单据或原件已收押凭证复印件',
								code: 'SDMS_0502_34'
							},
							{
								name: arr[i].mortgageSystemId,
								label: '抵（质）押物保险单或原件已收押凭证复印件',
								code: 'SDMS_0502_35'
							},
							{
								name: arr[i].mortgageSystemId,
								label: '抵质押评估材料',
								code: 'SDMS_0502_36'
							},
							{
								name: arr[i].mortgageSystemId,
								label: '抵（质）押其他材料',
								code: 'SDMS_0502_37'
							},
							
						]
					}
					let flag = this.treeData[0].children.find(item => item.label == arr[i].mortgageSystemId)
					if (!flag) {
						this.treeData[0].children.push(item)
					}


				}
				
				//数据更新时，更新树目录
				let arrAll = this.treeData[0].children
				let delArr = this.subSet(arr,arrAll)
				for(let m=0;m<delArr.length;m++){
					this.treeData[0].children.remove(delArr[m])
				}
			}
		}
	}
</script>

<style>

</style>
